﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;

///Author: NHDuc
///Created date: 26/11/2013
namespace VietThuongLMS.Services
{
    public class ChiNhanhService
    {
        public static List<ChiNhanh> GetChiNhanhList()
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<ChiNhanh> list = context.ChiNhanhs.ToList();
                return list;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetChiNhanhList", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static ChiNhanhViewModel GetChiNhanhList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<ChiNhanh> list = context.ChiNhanhs.OrderBy(c => c.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.ChiNhanhs.Count();
                return new ChiNhanhViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetChiNhanhList", ex.StackTrace, ex.Message);
                return new ChiNhanhViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertChiNhanh(ChiNhanh comp)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                context.AddToChiNhanhs(comp);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("ChiNhanhServices::InsertChiNhanh", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(ChiNhanh newModel)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                ChiNhanh model = context.ChiNhanhs.Where(c => c.ID == newModel.ID).FirstOrDefault();
                if (model != null)
                {
                    model.TenChiNhanh = newModel.TenChiNhanh;
                    model.DiaChi = newModel.DiaChi;
                    model.MoTa = newModel.MoTa;
                    if (newModel.UrlAvatar != null && newModel.UrlAvatar != "")
                        model.UrlAvatar = newModel.UrlAvatar;
                    if (newModel.UrlAvatar2 != null && newModel.UrlAvatar2 != "")
                        model.UrlAvatar2 = newModel.UrlAvatar2;
                    if (newModel.UrlAvatar3 != null && newModel.UrlAvatar3 != "")
                        model.UrlAvatar3 = newModel.UrlAvatar3;
                    if (newModel.UrlAvatar4 != null && newModel.UrlAvatar4 != "")
                        model.UrlAvatar4 = newModel.UrlAvatar4;
                    if (newModel.UrlAvatar5 != null && newModel.UrlAvatar5 != "")
                        model.UrlAvatar5 = newModel.UrlAvatar5;
                    if (newModel.ImgDiaDiem != null && newModel.ImgDiaDiem != "")
                        model.ImgDiaDiem = newModel.ImgDiaDiem;
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("ChiNhanhServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static ChiNhanh GetChiNhanhById(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.ChiNhanhs.Where(c => c.ID == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("ChiNhanhServices::GetChiNhanhById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteChiNhanh(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                ChiNhanh obj = context.ChiNhanhs.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    context.DeleteObject(obj);
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("ChiNhanhServices::DeleteChiNhanh", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        /// <summary>
        /// Lay danh sach phan cong theo TKB
        /// | Author: NHDuc
        /// | Created Date: 18/12/2013
        /// </summary>
        /// <param name="maLopHoc"></param>
        /// <returns></returns>
        public static List<ChiNhanhJsonModel> GetChiNhanhJson()
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<ChiNhanhJsonModel> list = context.ChiNhanhs
                            .ToList().Select(s => new ChiNhanhJsonModel
                            {
                                id = s.ID,
                                tenchinhanh = s.TenChiNhanh
                            }).ToList();

                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("ChiNhanhServices::GetChiNhanhJson", ex.StackTrace, ex.Message);
                return null;
            }
        }
    }
}